#include "Stack.h"
#include <stdio.h>
#include <stdlib.h>

struct StackRecord {
    int TopOfStack;
    ElementType* arr;
    int Capacity;
};

int IsEmpty(Stack S) {
    if (S)
        return S->TopOfStack == -1 ? 1 : 0;
    else
        return 1;
}

int IsFull(Stack S) {
    return S->Capacity - 1 == S->TopOfStack;
}

Stack CreateStack(int N) {
    Stack s = (Stack)malloc(sizeof(struct StackRecord));
    s->TopOfStack = -1;
    s->Capacity = N;
    s->arr = (ElementType*)malloc(N * sizeof(ElementType));
    return s;
}

void DisposeStack(Stack S) {
    if (S) {
        free(S->arr);
        free(S);
    }
}

void MakeEmpty(Stack S) {
    if (S)
        S->TopOfStack = -1;
}

void Push(ElementType x, Stack S) {
    if (S) {
        if (!IsFull(S)) {
            S->arr[++S->TopOfStack] = x;
        }
    }
}

ElementType Pop(Stack S) {
    ElementType res;
    if (S) {
        res = S->arr[S->TopOfStack--];
        return res;
    }
    return -1;
}

ElementType Top(Stack S) {
    if (S) {
        return S->arr[S->TopOfStack];
    }
    return 0;
}

void TestStack() {
    Stack s = CreateStack(10);

    for (int i = 0; i < 10; i++) {
        Push(i, s);
    }

    while (!IsEmpty(s)) {
        printf("%d\n", Pop(s));
    }

    DisposeStack(s);
}